package com.lizicloud.infrastructure.common.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 响应工具类
 */
public class ResponseUtils {
    
    /**
     * 创建成功响应
     */
    public static <T> Map<String, Object> success(T data) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "操作成功");
        response.put("data", data);
        response.put("success", true);
        return response;
    }
    
    /**
     * 创建成功响应（无数据）
     */
    public static Map<String, Object> success() {
        return success(null);
    }
    
    /**
     * 创建成功响应（带消息）
     */
    public static <T> Map<String, Object> success(T data, String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", message);
        response.put("data", data);
        response.put("success", true);
        return response;
    }
    
    /**
     * 创建错误响应
     */
    public static Map<String, Object> error(int code, String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", code);
        response.put("message", message);
        response.put("data", null);
        response.put("success", false);
        return response;
    }
    
    /**
     * 创建错误响应（默认错误码）
     */
    public static Map<String, Object> error(String message) {
        return error(500, message);
    }
    
    /**
     * 创建参数错误响应
     */
    public static Map<String, Object> badRequest(String message) {
        return error(400, message);
    }
    
    /**
     * 创建未找到错误响应
     */
    public static Map<String, Object> notFound(String message) {
        return error(404, message);
    }
    
    /**
     * 创建未授权错误响应
     */
    public static Map<String, Object> unauthorized(String message) {
        return error(401, message);
    }
    
    /**
     * 创建分页响应
     */
    public static <T> Map<String, Object> page(T data, long total, int pageNum, int pageSize) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", data);
        response.put("success", true);
        
        Map<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("total", total);
        pageInfo.put("pageNum", pageNum);
        pageInfo.put("pageSize", pageSize);
        pageInfo.put("totalPages", PageUtils.calculateTotalPages(total, pageSize));
        pageInfo.put("hasPrevious", PageUtils.hasPreviousPage(pageNum));
        pageInfo.put("hasNext", PageUtils.hasNextPage(pageNum, PageUtils.calculateTotalPages(total, pageSize)));
        
        response.put("pageInfo", pageInfo);
        return response;
    }
    
    private ResponseUtils() {
        // 私有构造函数，防止实例化
    }
}